package cn.edu.jxau.lang;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * Desc:
 * ------------------------------------
 * Author:fulei04@meituan.com
 * Date:2017/12/21
 * Time:上午11:49
 */
public class AuthInterceptor implements MethodInterceptor {

    private String username;

    public AuthInterceptor(String username) {
        this.username = username;
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {

        if(accept()) {
            return methodProxy.invokeSuper(obj,args);
        } else {
            throw new RuntimeException(String.format("你没有权限执行%s()",method.getName()));
        }
    }

    private boolean accept() {

        return "Fudashi".equals(username);
    }
}
